<script lang="ts" setup>
import {ref} from "vue"
import {useRouter} from 'vue-router'
import {getCategoryApi, mainNews} from "../serve"
import pagination from "./pagination.vue"
import {Search} from "@element-plus/icons-vue";

const
    router = useRouter(),
    itemData = ref({list: [], current: 1, size: 10, total: 0}),
    fromData = ref({size: 10, page: 1, categoryId: null, keyword: ''}),
    categories = ref<any[]>([]),
    load = ref(true)
;

function get() {
  load.value = true;
  mainNews(fromData.value).then((r: any) => {
    itemData.value = r
    load.value = false
    console.log(r)
  })
}

function getCategory() {
  getCategoryApi({
    page: 1,
    size: 99999,
    type: "NEWS"
  }).then((r: any) => {
    console.log(r);
    categories.value = r.list;
  })
}

function reset() {
  fromData.value.categoryId = null;
  fromData.value.keyword = ''
  get();
}

get()

function sizeChange(size: number) {
  fromData.value.size = size
  get()
}

function currentChange(currPage: number) {
  fromData.value.page = currPage
  get()
}

function openNewTab(id: any) {
  const r = router.resolve({path: '/information-details', query: {id}})
  window.open(r.href, '_blank')
}
</script>

<template>
  <el-scrollbar class="container">
    <div con v-loading="load" element-loading-text="加载中...">
      <div travel style="width: 70%">
        <div tr-top>
          <div dis>
            <div>旅游资讯</div>
          </div>
        </div>
        <div class="search_box">
          <el-input style="width: 240px" :prefix-icon="Search" v-model="fromData.keyword"
                    maxlength="20" show-word-limit
                    clearable
                    placeholder="请输入资讯题目"></el-input>
          <el-select v-model="fromData.categoryId"
                     filterable
                     :remote="true"
                     :remote-method="getCategory"
                     clearable
                     style="width: 240px;margin-left: 20px" placeholder="请选择类别">
            <el-option v-for="item in categories" :key="item.categoryId" :label="item.name" :value="item.categoryId"/>
          </el-select>
          <el-button style="margin-left: 20px" type="primary" icon="Search" @click="get">搜索</el-button>
          <el-button style="margin-left: 20px" icon="RefreshRight" @click="reset">重置</el-button>
        </div>
        <div class="news-list">
          <div v-for="(news, index) in itemData['list']" :key="index" class="news-item"
               @click="openNewTab(news['articleId'])">
            <div class="news-title">{{ news['title'] }}</div>
            <div class="news-date">{{ news['createdAt'] }}</div>
          </div>
        </div>
        <div page>
          <pagination :pageCurrent="itemData?.current" :pageSize="itemData?.size"
                      :small="false" :total="itemData?.total"
                      class="page" @size-change="sizeChange" @current-change="currentChange"/>
        </div>
      </div>
    </div>
    <div style="margin-top: 20px;">
      <div style="width: 98%;margin: auto;height: 1px;background-color: #002B49;"/>
      <div class="copyright">© 2025 旅游网站</div>
    </div>
  </el-scrollbar>
</template>

<style lang="scss" scoped>
.search_box {
  width: 100%;
  padding: 0 20px;
  line-height: 60px;
}

[page] {
  padding: 20px 100px;
}

.container {
  background-image: url('/bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: repeat-x;
}

[con] {
  padding-top: 30px;
}

[travel] {
  width: 90%;
  margin: auto;
  font-family: 'Microsoft YaHei', sans-serif;
  background-color: white;
  padding-bottom: 25px;
  border-radius: 6px;

  [tr-top] {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    background-color: #0062A5;
    color: #FFFFFF;
    padding: 25px;

    [dis] {
      font-size: 24px;
      font-weight: bold;
      padding-left: 10px;
      border-left: 4px solid #1890ff;

      div:nth-child(2) {
        font-size: 16px;
        font-weight: 400;
      }
    }
  }

  .route-list {
    margin: 30px 0;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    gap: 15px;
  }

  .route-card {
    width: 22%;
    height: 230px;
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background-size: cover;
    background-position: center;
    border-radius: 5px;
  }

  .card-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  }

  .news-list {
    //background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
  }

  .news-item {
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
  }

  .news-item:hover {
    background-color: #f0f0f0;
  }

  .news-item:last-child {
    border-bottom: none;
  }

  .news-date {
    color: #999;
    font-size: 15px;
    margin-top: 10px;
    min-width: 90px;
  }

  .news-title {
    color: #333;
    font-size: 20px;
  }
}

.divider {
  display: flex;
  align-items: center;
  margin: 50px 0 50px 0;
  justify-content: center;

  .divider-line {
    height: 3px;
    width: 100px;
    border-radius: 2px;
    background: #dcdfe6;
  }

  .divider-text {
    padding: 0 12px;
    //color: #8898aa;
    //font-size: 12px;
    color: #ffffff;
  }
}

.card-number {
  position: absolute;
  top: 12px;
  left: 12px;
  font-size: 28px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.9);
  z-index: 1;
}

.card-content {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px;
  color: white;
  z-index: 1;
}

.card-content h3 {
  font-size: 18px;
  margin: 0 0 8px;
}

.card-content p {
  font-size: 12px;
  line-height: 1.4;
  opacity: 0.9;
  margin: 0;
}


.attraction-list {
  display: flex;
  justify-content: flex-start;
  border-radius: 8px;
  padding: 15px 20px;
  flex-wrap: wrap;
  margin: auto;
}

.attraction-item {
  width: 40%;
  margin: 0 5% 50px 5%;
  height: 250px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  background-size: cover;
  background-position: center;
  border-radius: 5px;
  color: #ffffff;

}

.copyright {
  color: #002B49;
  font-size: 12px;
  padding: 20px;
  font-family: 'Microsoft YaHei', sans-serif;
}
</style>